<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <button id="execute">点击我执行命令</button>
    <button id="undo">点击我执行命令</button>
</body>
<script>

    // Mark:命令模式的意图是把请求封装为对象,
    //  从而分离请求的发起者和请求的接收者(执行者)之间的耦合关系。
    //  在命令被执行之前,可以预先往命令对象中植入命令的接收者。

    const Tv = {
        open: function () {
            console.log('打开电视机')
        },
        close: function () {
            console.log('关上电视机')
        }
    };

    const OpenTvCommand = function (receiver) {
        this.receiver = receiver;
    };

    OpenTvCommand.prototype.execute = function () {
        this.receiver.open();
    };

    OpenTvCommand.prototype.undo = function () {
        this.receiver.close();
    };

    const setCommand = function (command) {
        document.getElementById('execute').onclick = function () {
            command.execute();
        };
        document.getElementById('undo').onclick = function () {
            command.undo();
        };
    };

    setCommand(new OpenTvCommand(Tv));

</script>
</html>